﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using AwinDotNet;

public partial class controls_ProductList : System.Web.UI.UserControl
{

    private int _CatID = 0;
    private int _MerchatID = 0;
    private int _PageIndex = 1;
    private string _Query = null;
    private AwinDotNet.ProductSearch.SortOrder _SortOrder = AwinDotNet.ProductSearch.SortOrder.none;


    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString["cat"] != null)
        {
            int.TryParse(Request.QueryString["cat"].ToString(), out _CatID);
        }

        if (Request.QueryString["mer"] != null)
        {
            _MerchatID  = int.Parse(Request.QueryString["mer"]);
        }

        
        if (Request.QueryString["page"] != null)
        {
            _PageIndex = int.Parse(Request.QueryString["page"]);
        }
        if (_PageIndex < 1)
            _PageIndex = 1;

        if (Request.QueryString["q"] != null)
        {
            _Query = Request.QueryString["q"];
        }

        
        FillSortBox();
        

        
        _SortOrder = (AwinDotNet.ProductSearch.SortOrder) ( Enum.ToObject(typeof(AwinDotNet.ProductSearch.SortOrder),  int.Parse( ddSortBy.SelectedValue)));


        string query = (string)Request.QueryString["q"];

        FillGrid();

    }


    public void FillSortBox()
    {
        if (ddSortBy.Items.Count == 0)
        {
            ddSortBy.Items.Add(new ListItem("Relevence",((int) ProductSearch.SortOrder.Relevency).ToString() ));
            ddSortBy.Items.Add(new ListItem("Popularity", ((int)ProductSearch.SortOrder.PopularityDescending).ToString()));
            ddSortBy.Items.Add(new ListItem("Price: Low-High", ((int)ProductSearch.SortOrder.PriceAscending).ToString()));
            ddSortBy.Items.Add(new ListItem("Price: High-Low", ((int)ProductSearch.SortOrder.PriceDescending).ToString()));

            ddSortBy.Items.Add(new ListItem("Name: A-Z", ((int)ProductSearch.SortOrder.AlphaAscending).ToString()));
            ddSortBy.Items.Add(new ListItem("Name: Z-A", ((int)ProductSearch.SortOrder.AlphaDescending).ToString()));

            ddSortBy.SelectedValue = ((int)ProductSearch.SortOrder.PopularityDescending).ToString();

            
        }

    }


    public string ProductURL(object productID)
    {
        string url=@"/ShowProduct.aspx?cat={0}&id={1}";

        return string.Format(url, _CatID, productID);
               
    }

    public string ProductThumbNail(object productID)
    {
        
        AwinDotNet.Product p = AwinDotNet.AwinAPI.Products.GetProduct(long.Parse(productID.ToString()));

        return p.ThumbURL;
        
    }

    public string MerchantLink(object productID)
    {
        string hyp = @"<a href=""{0}"">{1}</a>";

        Product p = AwinDotNet.AwinAPI.Products.GetProduct(long.Parse(productID.ToString()));
        if (p != null)
        {
            Merchant m = AwinDotNet.AwinAPI.Merchants[p.MerchantID];

            hyp = string.Format(hyp, p.DeepLink, m.Name);

        }
        return hyp;
    }



    private void FillGrid()
    {
        AwinDotNet.ProductSearch ps = new ProductSearch();

        AwinDotNet.ProductCategory cat = AwinDotNet.AwinAPI.ProductCategories[_CatID];

        ps.RefineByCategories.Add(cat);
        ps.PageSize=10;
        ps.PageNumber = _PageIndex;
        ps.Sort = (ProductSearch.SortOrder) int.Parse(ddSortBy.SelectedValue); // ProductSearch.SortOrder.PopularityDescending;
        ps.HotPicks=false;
        ps.SearchSubCategories = false;
        ps.AdultContent=false;
        ps.SearchQuery = _Query;

        AwinDotNet.ProductSearchResults res = ps.Exectute();


        if (res.ProductCount > 0)
        {
            ListView1.DataSource = res.Products;
            ListView1.DataBind();
        }


        if ( res.ProductCount > 0)
        { //Draw the pager..

            StringBuilder sb = new StringBuilder();

            if (_PageIndex > 1)
                sb.Append(string.Format(@"<a href=""listproducts.aspx?cat={0}&page={1}"">{2}</a> ", _CatID, _PageIndex - 1, "<<"));

            sb.Append(" Page " + _PageIndex.ToString() + " of " + res.ProductCount.ToString());

            if (_PageIndex < res.PageCount)
                sb.Append(string.Format(@" <a href=""listproducts.aspx?cat={0}&page={1}"">{2}</a>",  _CatID,  _PageIndex + 1, ">>"));


            if (res.PageCount > 0)
            {
                lblPagerBottom.Text = sb.ToString();
                lblPagerTop.Text = sb.ToString();
                lblPagerTop.Visible = true;
                lblPagerBottom.Visible = true;
            }
            else
            {
                lblPagerTop.Text = "";
                lblPagerBottom.Text = "";

                lblPagerTop.Visible = false ;
                lblPagerBottom.Visible = false;
            }

        }


    }
}
